package com.sinosoft.cloud.bl;

import com.sinosoft.cloud.rest.TradeInfo;
import com.sinosoft.lis.entity.FeeCalFactorPojo;
import com.sinosoft.lis.pubfun.PubFun;
import com.sinosoft.utility.ExeSQL;
import com.sinosoft.utility.SSRS;
import com.sinosoft.utility.StrTool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class CalFactorBL extends AbstractBL {
    Log log = LogFactory.getLog(CalFactorBL.class);
    @Value("${riskCode.220702}")
    String sql_220702;
    @Value("${riskCode.123903}")
    String sql_123903;
    @Value("${riskCode.123703}")
    String sql_123703;
    @Value("${riskCode.221402}")
    String sql_221402;
    @Value("${riskCode.220802}")
    String sql_220802;
    @Value("${riskCode.124701}")
    String sql_124701;
    @Value("${riskCode.124302}")
    String sql_124302;




    public TradeInfo dealData(TradeInfo tradeInfo) {
        FeeCalFactorPojo feeCalFactorPojo = (FeeCalFactorPojo) tradeInfo.getData(FeeCalFactorPojo.class.getName());
        String riskCode = feeCalFactorPojo.getRiskCode();
        String prem = "";
        if ("220702".equals(riskCode)) {
            prem = cal220702(feeCalFactorPojo);
        } else if ("123903".equals(riskCode)) {
            prem = cal123903(feeCalFactorPojo);
        } else if ("123703".equals(riskCode)) {
            prem = cal123703(feeCalFactorPojo);
        } else if ("221402".equals(riskCode)) {
            prem = cal221402(feeCalFactorPojo);
        } else if ("220802".equals(riskCode)) {
            prem = cal220802(feeCalFactorPojo);
        } else if ("124701".equals(riskCode)) {
            prem = cal124701(feeCalFactorPojo);
        } else if ("124302".equals(riskCode)) {
            prem = cal124302(feeCalFactorPojo);
        }
        if ("".equals(prem)) {
            tradeInfo.addError("你都录了什么鬼，没算出来，哈哈哈哈");
            return tradeInfo;
        }
        tradeInfo.addData("feeResult", prem);
        return tradeInfo;
    }


    String cal220702(FeeCalFactorPojo feeCalFactorPojo) {
        ExeSQL exeSQL = new ExeSQL();
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("Get", feeCalFactorPojo.getAmnt());
        factorMap.put("Sex", feeCalFactorPojo.getGender());
        factorMap.put("AppAge", feeCalFactorPojo.getAge());

        SSRS ssrs = exeSQL.execSQL(getSql(sql_220702, factorMap));
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("220702算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }
    String cal124302(FeeCalFactorPojo feeCalFactorPojo) {
        ExeSQL exeSQL = new ExeSQL();
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("RnewFlag", feeCalFactorPojo.getXbFlag());
        factorMap.put("RiskWrapCode", feeCalFactorPojo.getRiskWrap());
        factorMap.put("AppAge", feeCalFactorPojo.getAge());
        factorMap.put("PayIntv", feeCalFactorPojo.getPayIntv());
        factorMap.put("Mult", feeCalFactorPojo.getLevel());
        String sql = getSql(sql_124302, factorMap);
        log.info("124302解释后的算法：："+sql);
        SSRS ssrs = exeSQL.execSQL(sql);
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("124302算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }
    String cal124701(FeeCalFactorPojo feeCalFactorPojo) {
        ExeSQL exeSQL = new ExeSQL();
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("AppAge", feeCalFactorPojo.getAge());
        factorMap.put("Mult", feeCalFactorPojo.getLevel());
        factorMap.put("Rnewflag", feeCalFactorPojo.getXbFlag());
        factorMap.put("PayIntv", feeCalFactorPojo.getPayIntv());

        SSRS ssrs = exeSQL.execSQL(getSql(sql_124701, factorMap));
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("124701算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }
    String cal220802(FeeCalFactorPojo feeCalFactorPojo) {
        ExeSQL exeSQL = new ExeSQL();
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("Get", feeCalFactorPojo.getAmnt());
        factorMap.put("Sex", feeCalFactorPojo.getGender());
        factorMap.put("AppAge", feeCalFactorPojo.getAge());

        SSRS ssrs = exeSQL.execSQL(getSql(sql_220802, factorMap));
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("220802算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }

    String cal123903(FeeCalFactorPojo feeCalFactorPojo) {
        ExeSQL exeSQL = new ExeSQL();

        log.info("123903保费计算：：：" + sql_123903);
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("Mult", feeCalFactorPojo.getLevel());
        factorMap.put("Rnewflag", feeCalFactorPojo.getXbFlag());
        factorMap.put("AppAge", feeCalFactorPojo.getAge());
        factorMap.put("PayIntv", feeCalFactorPojo.getPayIntv());
        String trueSql = getSql(sql_123903, factorMap);
        log.info("解释后的算法：：" + trueSql);
        SSRS ssrs = exeSQL.execSQL(trueSql);
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("123903算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }

    String cal123703(FeeCalFactorPojo feeCalFactorPojo) {
        log.info("123703保费计算：：：" + sql_123703);
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("Mult", feeCalFactorPojo.getLevel());
        factorMap.put("Rnewflag", feeCalFactorPojo.getXbFlag());
        factorMap.put("AppAge", feeCalFactorPojo.getAge());
        factorMap.put("PayIntv", feeCalFactorPojo.getPayIntv());

        ExeSQL exeSQL = new ExeSQL();
        String trueSql = getSql(sql_123703, factorMap);
        log.info("123703解释后的算法：：" + trueSql);
        SSRS ssrs = exeSQL.execSQL(trueSql);
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("123703算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }
    String cal221402(FeeCalFactorPojo feeCalFactorPojo) {
        log.info("221402保费计算：：：" + sql_221402);
        String amnt = "";
        if("1".equals(feeCalFactorPojo.getLevel())){
            amnt = "100000";
        }else if("2".equals(feeCalFactorPojo.getLevel())){
            amnt = "300000";
        }else if("3".equals(feeCalFactorPojo.getLevel())){
            amnt = "500000";
        }
        HashMap<String, String> factorMap = new HashMap<>();
        factorMap.put("Get", amnt);
        factorMap.put("Sex", feeCalFactorPojo.getGender());
        factorMap.put("AppAge", feeCalFactorPojo.getAge());

        ExeSQL exeSQL = new ExeSQL();
        String trueSql = getSql(sql_221402, factorMap);
        log.info("221402解释后的算法：：" + trueSql);
        SSRS ssrs = exeSQL.execSQL(trueSql);
        if (ssrs.MaxRow == 0) {
            return "";
        }
        String prem = ssrs.GetText(1, 1);
        log.info("221402算费结果：：：" + prem);
        return String.valueOf((int)(Double.parseDouble(prem)*100));
    }

    String getSql(String sql, Map<String, String> keyMap) {
        String tStr = "", tStr1 = "";
        while (true) {
            tStr = PubFun.getStr(sql, 2, "?");
            if (tStr.equals("")) {
                break;
            }
            tStr1 = "?" + tStr.trim() + "?";
            //替换变量
            sql = StrTool.replaceEx(sql, tStr1, keyMap.get(tStr));
        }
        return sql;
    }
}
